/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Aprendiz;
import co.edu.senaclem.modelo.Coordinador;
import co.edu.senaclem.modelo.Instructor;
import co.edu.senaclem.modelo.Permiso;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DILOVE
 */
@Stateless
public class PermisoDAO implements IPermisoDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void registrar(Permiso permiso) throws Exception {
        //System.out.println("Permiso en en DAO "+permiso.toString());
        try {
            entityManager.persist(permiso);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Permiso permiso) throws Exception {
        try {
            entityManager.merge(permiso);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void eliminar(Permiso permiso) throws Exception {
        try {
            entityManager.remove(permiso);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Permiso consultarPorId(Integer codigopermiso) throws Exception {
        try {
            return entityManager.find(Permiso.class, codigopermiso);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Permiso> consultar() throws Exception {
        try {
            String jpql = "Select p From Permiso p";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Permiso> consultarPermisosDiarios(String fecha) throws Exception {
        try {
            String jpql = "select p from Permiso p where p.fechapermiso = '" + fecha + "';";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public String generarCodigoPermiso() throws Exception {
        int max = 0;
        try {
            String jpql = "select max(codigopermiso) as maxcp from permiso;";
            Query query = entityManager.createQuery(jpql);
            max = Integer.parseInt(query.getSingleResult().toString());
            max = max + 1;
            return max + "";
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Permiso> consultarPermisosDiariosIns(String fechaSistema, Instructor ins) throws Exception {
        try {
            String jpql = "select p from Permiso p, Aprendiz a, Fichatitulacion f where p.fechapermiso='" + fechaSistema + "' and p.documentoaprendiz.documentoaprendiz=a.documentoaprendiz and a.fichatitulacion.fichatitulacion=fichatitulacion.fichatitulacion and f.fichatitulacion IN (select f.fichatitulacion from Fichatitulacion f, Instructor i, Asignafichas a where i.documentoinstructor=" + ins.getDocumentoinstructor() + " and a.documentoinstructor.documentoinstructor=i.documentoinstructor and a.fichatitulacion.fichatitulacion=fichatitulacion.fichatitulacion)";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Permiso> consultarPermisosAprendiz(Long documentoAprendiz) throws Exception {
        try {
            String jpql = "Select p From Permiso p where p.documentoaprendiz.documentoaprendiz=" + documentoAprendiz + ";";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void aprobarPermiso(Permiso permiso) throws Exception {
        try {
            entityManager.merge(permiso);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void aprobarPermisoC(Permiso permiso) throws Exception {
        try {
            entityManager.merge(permiso);
        } catch (RuntimeException e) {
            throw e;
        }
    }

}
